package com.ruoyi.project.shop.controller;


import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mall.domain.MallShopUserDO;
import com.ruoyi.project.mall.service.MallShopUserService;
import com.ruoyi.project.shop.domain.ShopShopDO;
import com.ruoyi.project.shop.domain.ShopUserDO;
import com.ruoyi.project.shop.domain.ShopUserRoleDO;
import com.ruoyi.project.shop.service.ShopShopService;
import com.ruoyi.project.shop.service.ShopUserRoleService;
import com.ruoyi.project.shop.service.ShopUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 商户信息表
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2021-01-22 15:20:48
 */
 
@Controller
@RequestMapping("/shop/shopUser")
public class ShopUserController extends BaseController {
	@Autowired
	private ShopUserService shopUserService;
	@Autowired
	private MallShopUserService mallShopUserService;
	@Autowired
	private ShopShopService shopShopService;
	@Autowired
	private ShopUserRoleService shopUserRoleService;


	/*@GetMapping()
	//@RequiresPermissions("shop:shopUser:shopUser")
	String ShopUser(){
	    return "shop/shopUser/shopUser";
	}*/
	
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('shop:shopUser:shopUser')")
	@GetMapping("/list")
	public TableDataInfo list(ShopUserDO shopUserDO) {
	    /*params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&& StringUtils.isNotBlank(params.get("name").toString())){
            List<ShopUserDO> shopUserList = shopUserService.likeList(query);
            int total = shopUserService.likeListCount(query);
            PageUtils pageUtils = new PageUtils(shopUserList, total);
            return pageUtils;
		}

		List<ShopUserDO> shopUserList = shopUserService.list(query);
		int total = shopUserService.count(query);
		PageUtils pageUtils = new PageUtils(shopUserList, total);
		return pageUtils;*/
		startPage();
		shopUserDO.setDelFlag("0");
		shopUserDO.setStatus("0");
		List<ShopUserDO> list = shopUserService.likeList(BeanUtil.beanToMap(shopUserDO));
		return getDataTable(list);
	}
	
	/*@GetMapping("/add")
	@RequiresPermissions("shop:shopUser:add")
	String add(){
	    return "shop/shopUser/add";
	}*/

	/*@GetMapping("/edit/{suserId}")
	@RequiresPermissions("shop:shopUser:edit")
	String edit(@PathVariable("suserId") Long suserId,Model model){
		ShopUserDO shopUser = shopUserService.get(suserId);
		model.addAttribute("shopUser", shopUser);
	    return "shop/shopUser/edit";
	}*/
	
	/**
	 * 保存
	 *//*
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:shopUser:add")
	public R save(ShopUserDO shopUser){

		if(shopUserService.save(shopUser)>0){
			return R.ok();
		}
		return R.error();
	}*/


	/**
	 * 获取商家账号信息详细信息
	 *//*
	@PreAuthorize("@ss.hasPermi('shop:shopUser:query')")
	@GetMapping(value = "/{id}")
	@ResponseBody
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		ShopUserDO shopAdminByShopId = shopUserService.get(Long.parseLong(id));
		return AjaxResult.success(shopAdminByShopId);
	}*/

//	@PreAuthorize("@ss.hasPermi('shop:shopUser:query')")
	/*@GetMapping(value = "/getUserByShopId/{id}")
	@ResponseBody
	public AjaxResult getUserByShopId(@PathVariable("id") String id)
	{
		ShopUserDO shopAdminByShopId = shopUserService.getShopAdminByShopId(id);
		return AjaxResult.success(shopAdminByShopId);
	}*/

	/**
	 * 添加/查询商家入驻信息
	 */
	/*@PreAuthorize("@ss.hasPermi('mall:mallShopUser:add')")
	@Log(title = "商家入驻信息", businessType = BusinessType.UPDATE)
	@GetMapping
	public AjaxResult edit(@RequestBody ShopUserDO shopUser)
	{
		if(shopUser.getSuserId()==null){
			Map<String,Object> map = new HashMap<>();
			map.put("userName",shopUser.getUserName());
			map.put("delFlag","0");
			List<MallShopUserDO> list = mallShopUserService.list(map);
			if(list.size()>0){
				return error("商家信息有误");
			}
			shopUser.setUserType("0");
			shopUser.setDelFlag("0");
			shopUser.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId()+"");
			shopUser.setPassword(SecurityUtils.encryptPassword(shopUser.getPassword()));
			shopUser.setCreateTime(new Date());
			//查询申请时得数据添加到shopshop表里
			MallShopUserDO mallShopUserDO = mallShopUserService.get(shopUser.getShopId());
			ShopShopDO shopShopDO = new ShopShopDO();
			shopShopDO.setAddress(mallShopUserDO.getAddress());
			shopShopDO.setAllMoney(new BigDecimal("0.00"));
			shopShopDO.setCheck(mallShopUserDO.getStatus());
			shopShopDO.setId(mallShopUserDO.getId());
			shopShopDO.setCoordinate(mallShopUserDO.getName());
			shopShopDO.setDelFlag(0);
			shopShopDO.setDisabled(0);
			shopShopDO.setCreateAt(new Date());
			shopShopDO.setImg(mallShopUserDO.getLogo());
			shopShopDO.setIntroduce(mallShopUserDO.getIntro());
			shopShopDO.setMobile(mallShopUserDO.getMobile());
			shopShopDO.setMoney(new BigDecimal("0.00"));
			shopShopDO.setName(mallShopUserDO.getShopName());
			shopShopDO.setParentId(mallShopUserDO.getId());
			shopShopDO.setPhone(mallShopUserDO.getPhone());
			shopShopDO.setSale(0);
			shopShopDO.setShareImg(mallShopUserDO.getImgurl());
			shopShopDO.setStatus(1);
			shopShopDO.setSubheading(mallShopUserDO.getScale());
			shopShopService.save(shopShopDO);
			mallShopUserDO.setServerId(shopUser.getServerId());
			mallShopUserService.update(mallShopUserDO);

			shopUserService.save(shopUser);
			ShopUserRoleDO shopUserRoleDO = new ShopUserRoleDO();
			shopUserRoleDO.setSuserId(shopUser.getSuserId());
			shopUserRoleDO.setRoleId(5L);
			return toAjax(shopUserRoleService.save(shopUserRoleDO));
		}else{
			shopUser.setPassword(SecurityUtils.encryptPassword(shopUser.getPassword()));
			shopUser.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId()+"");
			shopUser.setUpdateTime(new Date());
			MallShopUserDO mallShopUserDO = mallShopUserService.get(shopUser.getShopId());
			mallShopUserDO.setServerId(shopUser.getServerId());
			mallShopUserService.update(mallShopUserDO);
			return toAjax(shopUserService.update(shopUser));
		}
		//mallShopUser.setUpAt(new Date());
		//return toAjax(mallShopUserService.update(mallShopUser));
	}*/

	/**
	 * 获取店铺用户详细信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:shopUser:query')")
	@GetMapping(value = "/{suserId}")
	public AjaxResult getInfo(@PathVariable("suserId") Long suserId)
	{
		return AjaxResult.success(shopUserService.get(suserId));
	}

	/**
	 * 新增店铺用户
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:shopUser:add')")
	@Log(title = "店铺用户", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody ShopUserDO shopUser)
	{
		shopUser.setUserType("0");
		shopUser.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId()+"");
		shopUser.setPassword(SecurityUtils.encryptPassword(shopUser.getPassword()));
		shopUser.setDelFlag("0");
		shopUser.setCreateTime(new Date());
		//查询申请时得数据添加到shopshop表里
		MallShopUserDO mallShopUserDO = mallShopUserService.get(shopUser.getShopId());

		ShopShopDO shopShopDO1 = shopShopService.get(mallShopUserDO.getId());
		if(shopShopDO1==null){
			ShopShopDO shopShopDO = new ShopShopDO();
			shopShopDO.setAddress(mallShopUserDO.getAddress());
			shopShopDO.setAllMoney(new BigDecimal("0.00"));
			shopShopDO.setCheck(mallShopUserDO.getStatus());
			shopShopDO.setId(mallShopUserDO.getId());
			shopShopDO.setCoordinate(mallShopUserDO.getName());
			shopShopDO.setDelFlag(0);
			shopShopDO.setDisabled(0);
			shopShopDO.setCreateAt(new Date());
			shopShopDO.setImg(mallShopUserDO.getLogo());
			shopShopDO.setIntroduce(mallShopUserDO.getIntro());
			shopShopDO.setMobile(mallShopUserDO.getMobile());
			shopShopDO.setMoney(new BigDecimal("0.00"));
			shopShopDO.setName(mallShopUserDO.getShopName());
			shopShopDO.setParentId(mallShopUserDO.getId());
			shopShopDO.setPhone(mallShopUserDO.getPhone());
			shopShopDO.setSale(0);
			shopShopDO.setShareImg(mallShopUserDO.getImgurl());
			shopShopDO.setStatus(1);
			shopShopDO.setSubheading(mallShopUserDO.getScale());
			shopShopService.save(shopShopDO);
		}

		mallShopUserDO.setServerId(shopUser.getServerId());
		mallShopUserService.update(mallShopUserDO);

		shopUserService.save(shopUser);
		ShopUserRoleDO shopUserRoleDO = new ShopUserRoleDO();
		shopUserRoleDO.setSuserId(shopUser.getSuserId());
		shopUserRoleDO.setRoleId(5L);
		shopUserRoleService.save(shopUserRoleDO);
		return toAjax(1);
	}

	/**
	 * 修改店铺用户
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:shopUser:edit')")
	@Log(title = "店铺用户", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody ShopUserDO shopUser)
	{
		shopUser.setPassword(SecurityUtils.encryptPassword(shopUser.getPassword()));
		shopUser.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId()+"");
		shopUser.setUpdateTime(new Date());
		MallShopUserDO mallShopUserDO = mallShopUserService.get(shopUser.getShopId());
		mallShopUserDO.setServerId(shopUser.getServerId());
		mallShopUserService.update(mallShopUserDO);
		return toAjax(shopUserService.update(shopUser));
	}

	/**
	 * 删除店铺用户
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:shopUser:remove')")
	@Log(title = "店铺用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{suserIds}")
	public AjaxResult remove(@PathVariable Long[] suserIds)
	{
		/*if(ids.length>0){
			for (String string : ids) {
				ShopUserDO shopUser = shopUserService.get(string);
				shopUser.setDelFlag(1);
				shopUser.setUpAt(new Date());
				shopUserService.update(shopUser);
			}
		}*/
		return toAjax(shopUserService.batchRemove(suserIds));
		//return toAjax(shopUserService.deleteShopUserByIds(suserIds));
	}
	/**
	 * 修改
	 *//*
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:shopUser:edit")
	public R update( ShopUserDO shopUser){
	if(shopUser.getSuserId()==null){
		Map<String,Object> map = new HashMap<>();
		map.put("userName",shopUser.getUserName());
		map.put("delFlag","0");
		List<MallShopUserDO> list = mallShopUserService.list(map);
		if(list.size()>0){
			return R.error("用户名重复");
		}
			shopUser.setUserType("0");
			shopUser.setDelFlag("0");
			shopUser.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId()+"");
			shopUser.setPassword(SecurityUtils.encryptPassword(shopUser.getPassword()));
			shopUser.setCreateTime(new Date());
			//查询申请时得数据添加到shopshop表里
			MallShopUserDO mallShopUserDO = mallShopUserService.get(shopUser.getShopId());
			ShopShopDO shopShopDO = new ShopShopDO();
			shopShopDO.setAddress(mallShopUserDO.getAddress());
			shopShopDO.setAllMoney(new BigDecimal("0.00"));
			shopShopDO.setCheck(mallShopUserDO.getStatus());
			shopShopDO.setId(mallShopUserDO.getId());
			shopShopDO.setCoordinate(mallShopUserDO.getName());
			shopShopDO.setDelFlag(0);
			shopShopDO.setDisabled(0);
			shopShopDO.setCreateAt(new Date());
			shopShopDO.setImg(mallShopUserDO.getLogo());
			shopShopDO.setIntroduce(mallShopUserDO.getIntro());
			shopShopDO.setMobile(mallShopUserDO.getMobile());
			shopShopDO.setMoney(new BigDecimal("0.00"));
			shopShopDO.setName(mallShopUserDO.getShopName());
			shopShopDO.setParentId(mallShopUserDO.getId());
			shopShopDO.setPhone(mallShopUserDO.getPhone());
			shopShopDO.setSale(0);
			shopShopDO.setShareImg(mallShopUserDO.getImgurl());
			shopShopDO.setStatus(1);
			shopShopDO.setSubheading(mallShopUserDO.getScale());
			shopShopService.save(shopShopDO);
			mallShopUserDO.setServerId(shopUser.getServerId());
			mallShopUserService.update(mallShopUserDO);

			shopUserService.save(shopUser);
			ShopUserRoleDO shopUserRoleDO = new ShopUserRoleDO();
			shopUserRoleDO.setSuserId(shopUser.getSuserId());
			shopUserRoleDO.setRoleId(5L);
			shopUserRoleService.save(shopUserRoleDO);
		}else{
			shopUser.setPassword(SecurityUtils.encryptPassword(shopUser.getPassword()));
			shopUser.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId()+"");
			shopUser.setUpdateTime(new Date());
			MallShopUserDO mallShopUserDO = mallShopUserService.get(shopUser.getShopId());
			mallShopUserDO.setServerId(shopUser.getServerId());
			mallShopUserService.update(mallShopUserDO);
			shopUserService.update(shopUser);
		}

		return R.ok();
	}*/
	
	/**
	 * 删除
	 */
	/*@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:shopUser:remove")
	public R remove( Long suserId){
		ShopUserDO shopUser = shopUserService.get(suserId);

		if(shopUserService.update(shopUser)>0){
		return R.ok();
		}
		return R.error();
	}
	
	*//**
	 * 删除
	 *//*
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:shopUser:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] suserIds){
		shopUserService.batchRemove(suserIds);
		return R.ok();
	}
	*/
}
